﻿using System;
using System.Collections.Generic;
using System.Text;
using Fallin.Entities;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;

namespace Fallin.Helpers
{
    public class DataBaseHelper
    {
        const string DataBaseFileName = "DataBase.db";

        private static string GetPath()
        {
            return string.Format("{0}\\{1}", Assembly.GetExecutingAssembly().Location, DataBaseFileName);
        }

        public static void SaveData(IDictionary<string, FolderInfoEntity> dict)
        {
            using (Stream s = new FileStream(GetPath(), FileMode.OpenOrCreate, FileAccess.Write))
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(s, dict);
            }
        }

        public static IDictionary<string, FolderInfoEntity> GetData()
        {
            using (Stream s = new FileStream(GetPath(), FileMode.OpenOrCreate, FileAccess.Read))
            {
                BinaryFormatter bf = new BinaryFormatter();
                return (IDictionary<string, FolderInfoEntity>) bf.Deserialize(s);
            }
        }
    }
}
